package com.bank.accountinfo.user.controller;

import com.bank.setting.standard.result.Result;
import com.bank.accountinfo.user.entity.User;
import com.bank.accountinfo.user.service.UserService;
import com.bank.setting.standard.utils.JwtUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "http://localhost:8081") // 允许来自该源的请求
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtil jwtUtil;

    // 获取单个用户
    @GetMapping("/{id}")
    public User getUserById(@PathVariable Long id) {
        return userService.getUserById(id);
    }

    // 添加用户
    @PostMapping("/add")
    public boolean addUser(@RequestBody User user) {
        return userService.addUser(user);
    }

    // 更新用户
    @PutMapping("/updateById")
    public boolean updateUser( @RequestBody User user) {

        return userService.updateUser(user);
    }

    // 删除用户
    @DeleteMapping("/{id}")
    public boolean deleteUserById(@PathVariable Long id) {
        return userService.deleteUserById(id);
    }

    // 获取所有用户
    @GetMapping
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }

    // 批量插入用户
    @PostMapping("/batch")
    public boolean batchInsert(@RequestBody List<User> userList) {
        return userService.batchInsert(userList);
    }

    // 其他接口...
    @GetMapping("/page")
    public Page<User> getUserPage(@RequestParam(value = "page", defaultValue = "1") int page,
                                  @RequestParam(value = "size", defaultValue = "10") int size) {
        Page<User> userPage = new Page<>(page, size);
        return userService.getUserPage(userPage);
    }

    @PostMapping("/login")
    public Result<Map<String, Object>> login(@Validated @RequestBody User user, HttpServletResponse response) {
        // 调用服务层进行登录
        // ...
        Map<String, Object> data = userService.login(user);
        if (data != null) {
            response.setHeader("Token", (String) data.get("token"));
            response.setHeader("Access-Control-Expose-Headers", "Token");
            return Result.success(data);
        }
        return Result.fail(2002, "用户名或密码错误");
    }

    //
//    @PostMapping("/register")
//    public ResponseEntity<?> register(@RequestBody UserRegistrationRequest registrationRequest) {
//        // 处理注册逻辑
//        // ...
//        return ResponseEntity.ok().body("Registration successful");
//    }
    @PostMapping("/current")
    public Claims getCurrentUser(HttpServletRequest request) {
        String token=request.getHeader("Token");
        return jwtUtil.parseToken(token);
    }
}
